<?php
class b2c_apiv_apis_response_goods_product
{
    //公开的构造函数
    public function __construct($app)
    {
        $this->app=$app;
    }    

    
    public function get_store($params, &$service)
    {
        $jsonProductId = $params['product_id'];
        $productIds = json_decode($jsonProductId,true);
        if(count($productIds)>20 || count($productIds) < 1)
        {
            return $service->send_user_error('参数错误');
        }

        $obj_products = $this->app->model('products');
        $products_filter = array('product_id'=>$productIds);
        $products = $obj_products->getList('product_id,goods_id,store,freez',$products_filter);

        if(count($products) == 0) return array('status'=>'error','message'=>'没有找到任何一个货品');

        foreach($products as $key=>$product)
        {
            $fmt_products[$product['product_id']] = $product;
            $goods_ids[$key] = $product['goods_id'];
        }

        $obj_goods = $this->app->model('goods');
        $goodsdata = $obj_goods->getList('goods_id,nostore_sell,store',array('goods_id'=>$goods_ids));
        foreach($goodsdata as $goodsRow)
        {
            $fmt_goods[$goodsRow['goods_id']] = $goodsRow;
        }

        foreach($fmt_products as $fmt_product)
        {
            if( $fmt_goods[$fmt_product['goods_id']] && ($fmt_goods[$fmt_product['goods_id']]['nostore_sell'] || $fmt_goods[$fmt_product['goods_id']]['store']==null))
                $return[$fmt_product['product_id']] = 999999;
            else
                $return[$fmt_product['product_id']] = $fmt_product['store'] - $fmt_product['freez'];
        }
        
        return $return;
    }
    
    public function get_products($sdf, &$service)
    {
        $page_no = intval($sdf['page_no']);
        $page_size = intval($sdf['page_size']);
        $page_no = max(1, $page_no);
        if( ! $page_size) $page_size = 20;
        $offset = ($page_no - 1) * $page_size;

        
        $db = kernel::database();

        $rs = $db->select("SELECT count(*) as count FROM `" . DB_PREFIX . "b2c_products` WHERE 1");
        if(!$rs[0]['count']){
            return array('total_results'=>0, 'items'=>'[]');            
        }
        
        $sql = "SELECT * FROM `" . DB_PREFIX . "b2c_products` WHERE 1 ORDER BY product_id ASC LIMIT {$offset},{$page_size}";
        $rows = $db->select($sql);
        if ( ! $rows){
            return array('total_results'=>0, 'items'=>'[]');
        }

        return array('total_results'=>$rs[0]['count'], 'items'=>json_encode($rows));
    }

    
    public function get_lv_price($params, &$service)
    {
        $jsonProductId = $params['product_id'];
        $productIds = json_decode($jsonProductId,true);
        if(count($productIds)>20 || count($productIds) < 1)
        {
            return $service->send_user_error('参数错误，请提交正确的参数');
        }
        $obj_products = $this->app->model('products');
        $obj_goods_lv_price = $this->app->model('goods_lv_price');
        $obj_member_lv = $this->app->model('member_lv');

        //获取货品你价格
        $filter1=array('product_id'=>$productIds);
        $product_ids=$obj_products->getList('product_id,price',$filter1);
        foreach($product_ids as $product_id)
        {
            $fmt_product_ids[$product_id['product_id']] = $product_id['product_id'];
            $fmt_product_prices[$product_id['product_id']] = $product_id['price'];
        }
        //获取会员详情
        $member_lvs = $obj_member_lv->getList('member_lv_id,name,dis_count');
        foreach($member_lvs as $member_lv)
        {
            $member_lv_id = $member_lv['member_lv_id'];
            $fmt_member_lv[$member_lv_id] = $member_lv;
        }
        //获取会员价
        $filter2 = array('product_id'=>$fmt_product_ids);
        $price_datas = $obj_goods_lv_price->getList('product_id,level_id,price', $filter2);
        foreach($price_datas as $price_data)
        {
            $fmt_lv_price[$price_data['product_id']][$price_data['level_id']] = $price_data['price'];
        }

        //组织数据
        if(is_array($fmt_product_ids))
        {
            foreach($fmt_product_ids as $product_id)
            {
                foreach($fmt_member_lv as $member_lv_id=>$member_lv)
                {
                    if(isset($fmt_lv_price[$product_id][$member_lv_id]) && $fmt_lv_price[$product_id][$member_lv_id])
                    {
                        $temp = $fmt_lv_price[$product_id][$member_lv_id];
                        $goods_member_price[$product_id][$member_lv['name']] = $temp;
                    }else{
                        $temp = $fmt_product_prices[$product_id] * $member_lv['dis_count'];
                        $goods_member_price[$product_id][$member_lv['name']] = $temp;
                    }
                }
            }
        }
        $return['prices'] = $goods_member_price;
        return $return;
    }
    
    function price_update(&$sdf, &$thisObj)
    {
        $error_bn = array();
        $true_bn = array();

        if(!isset($sdf['list_price']) || !$sdf['list_price']){
            $thisObj->send_user_error(app::get('b2c')->_('需要更新的货品的库存不存在！'), array());
        }else{
            $has_error = false;
            $arr_price = json_decode($sdf['list_price'], true);

            $product = $this->app->model('products');
            $obj_goods = $this->app->model('goods');
            $priceModel = kernel::single('b2c_goods_price');
            $fail_products = array();
            $db = kernel::database();
            if (isset($arr_price) && $arr_price)
            {
                foreach ($arr_price as $arr_product_info)
                {
                    if ($arr_product_info['bn'] && ($arr_product_info['price']))
                    {
                        $price = floatval($arr_product_info['price']);
                        $mkt_price = floatval($arr_product_info['mkt_price']);
                        
                        $arr_product_list = $product->getList("*",array('bn' => $arr_product_info['bn']));
                        $arr_product = $arr_product_list[0];
                        if ($arr_product) {
                            $priceTemp = array();
                            $sql = 'SELECT price,goods_id from ' . DB_PREFIX . 'b2c_goods where goods_id ='.$arr_product['goods_id'];
                            $arr_goods = $db->selectrow($sql); 
                            $update_arr = array(
                                "product_id" => $arr_product["product_id"],
                                'price' =>array(
                                    'price'=>array('price'=>$price),
                                    'cost' =>array('price'=>$price),
                                    'mktprice'=>array('price'=>$mkt_price),
                                ),
                                "last_modify" => time(),
                            );
                            $is_save = $product->save($update_arr);
                            if($is_save){
                                $arr_goods['price'] = $price;
                                $arr_goods['mktprice'] = $mkt_price;
                                $obj_goods->update($arr_goods, array('goods_id' => $arr_goods['goods_id']));
                                
                                $priceModel->saveSalesPrice(array(
                                    'goods_id'=>$arr_goods['goods_id'],
                                    'price'=>$price,
                                    'mktprice'=>$mkt_price,
                                ));
                            }

                            if( ! $is_save){
                                $msg = $this->app->_('商品价格更新失败！');
                                $has_error = true;

                                $fail_products[] = $arr_product_info['bn'];
                                $error_bn[] = $arr_product_info['bn'];
                                continue;
                            }else{
                                $true_bn[] = $arr_product_info['bn'];
                            }
                        }else{
                            $has_error = true;
                            $fail_products[] = $arr_product_info['bn'];
                            $error_bn[] = $arr_product_info['bn'];
                            continue;
                        }
                    }else{
                        $has_error = true;
                        continue;
                    }
                }

                if( ! $has_error){
                    $data = array('error_bn'=>$error_bn, 'true_bn'=>$true_bn);
                    return $data;
                    //return true;
                }else{
                    // 更新部分失败.
                    $data = array('error_bn'=>$error_bn, 'true_bn'=>$true_bn);
                    $fail_products = array('error_response' => $fail_products);
                    $thisObj->send_user_error(app::get('b2c')->_('部分更新失败！'), $data);
                }
            }else{
                $thisObj->send_user_error(app::get('b2c')->_('更新的商品信息不存在！'), array());
            }
        }



    }
}
